/**
 * 文件：ModifyAccomPanel.java
 * 说明：用于修改学生住宿信息
 * 作者：古通基
 * 时间：1211-09-14
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ModifyAccomPanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = 7050448629480146863L;
	private JLabel lno = new JLabel("学号：");                                  //静态标签
	private JLabel lname = new JLabel("姓名：");
	private JLabel lroom = new JLabel("宿舍：");              
	private JLabel lbed = new JLabel("床位：");  	   
	
	private JTextField no = new JTextField("请输入五位学号",10);      //文本输入
	private JTextField name = new JTextField("",10);         
	String[] rooms = {"6425","6426","6427","3625","3626","3627"};
    private JComboBox room = new JComboBox(rooms);    
    String[] beds = {"1","2","3","4","5","6","7","8"};
    private JComboBox bed = new JComboBox(beds);       

    private JButton query = new JButton("查询");                            //声明按钮
    private JButton ok = new JButton("确定");
    private JButton modify = new JButton("修改");
    	
    private JPanel JPcomon1 = new JPanel();                                   //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public ModifyAccomPanel()                                                        //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lroom.setHorizontalAlignment(SwingConstants.CENTER);
    	lbed.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lroom);
    	JPcomon3.add(room);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lbed);
    	JPcomon4.add(bed);
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(modify);
    	JPbutton.add(ok);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPbutton);
    	
    	query.addActionListener(this);                                                  //事件监听        
    	ok.addActionListener(this);
    	modify.addActionListener(this);
    	no.addMouseListener(this);
    }
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			no.setEditable(false);
			name.setEditable(false);
			room.setEnabled(false);
			bed.setEnabled(false);
			query.setEnabled(false);
		}
		else if(e.getActionCommand().equals("修改"))
		{
			room.setEnabled(true);
			bed.setEnabled(true);
		}
		else if(e.getActionCommand().equals("确定"))
		{
			Ok();
		    no.setEditable(true);
		    query.setEnabled(true);
		}
	}	
	
	private void QueryDataBase()
	{
		ResultSet rs = null;
		try
		{
			BaseSQL accom = null;
			accom = new BaseSQL();
			String sql = "select * from accom where sno = '"+Integer.parseInt(no.getText())+"'";  
		    rs = accom.executeSelect(sql);
		    if(rs.next())
    		{
    			if(Integer.parseInt(no.getText())==rs.getInt("sno"))
    			{
    				name.setText(rs.getString("sname"));
    				room.setSelectedItem((rs.getString("room")));
    				bed.setSelectedItem((rs.getString("bed")));				
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"不存在学号为："+no.getText()+" 住宿信息","查询错误",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	

	private void Ok()
	{
		try
		{
			BaseSQL accom = null;
			accom = new BaseSQL();
			String sql = "update accom" +
					" set sname='"+name.getText()+"',room='"+room.getSelectedItem()+"',bed = '"+bed.getSelectedItem()+"' where sno = '"+Integer.parseInt(no.getText())+"'";
			accom.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "修改成功", "修改住宿信息",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			System.out.println(evt);
		} 
		catch (ClassNotFoundException evt) 
		{
			evt.printStackTrace();
		}	
	}	
	
    public void mouseClicked(MouseEvent e) { }                                           //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入五位学号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}    	
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}

